<?php
namespace app\admin\controller;
use framework\ext\Check;
use framework\ext\Http;

class BackupController extends \app\base\controller\AdminController{

    //模板列表
    public function index(){
        $this->list = model('database')->dbList();
        $this->display();  
    } 

    //获取所有表
    public function tables(){
        $this->list = model('database')->DBTableList();
        $this->display();  
    }    

    //数据库备份
    public function backupdb(){
        $rel = model('database')->backup();
        if($rel == 1){
            $this->pageMsg('数据库备份操作执行完毕！',1);
        }else{
            $this->pageMsg($rel);
        }
    } 
    //数据库优化
    public function optimizeddb(){
        $rel = model('database')->optimized();
        if($rel == 1){
            $this->pageMsg('数据库数据库优化操作执行完毕！',1);
        }else{
            $this->pageMsg($rel);
        }
        
    } 
    //数据库修复
    public function repairdb(){
        $rel = model('database')->repair();
        if($rel == 1){
            $this->pageMsg('数据库修复操作执行完毕！',1);
        }else{
            $this->pageMsg($rel);
        } 
    } 

    //数据库恢复
    public function importdb(){
        $DBBakFiles = form('get.name');
        if(empty($DBBakFiles)){
            $this->pageMsg('参数不能为空！');
        }
        //获取备份数量
        if(model('database')->import($DBBakFiles)){
            $this->pageMsg('备份恢复成功！',1);
        }else{
            $rel = model('database')->DBError;
            if(empty($rel)){
                $this->pageMsg('备份恢复失败！');
            }else{
                $this->pageMsg($rel);
            }
        }
    }

    //删除备份
    public function delectdb(){
        $act = form('get.act');
        $name = form('get.name');
        if(empty($act) || empty($name)){
            $this->jsonMsg('参数不能为空！');
        }
        if(model('database')->DelectDB($name)){
            $this->jsonMsg('备份文件删除完毕！',1);
        }else{
            $this->jsonMsg('删除失败');
        }
        
    }

    //下载备份
    public function downdb(){
        $name = form('get.name');
        if(empty($name)){
            $this->jsonMsg('参数不能为空！');
        }
        $rel = model('database')->downdb($name);
        if($rel){
            Http::download($rel['path'],$rel['name']);
        }else{
            $this->jsonMsg('备份文件不存在');
        }
    }
}